/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dao;

import domain.User;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;

/**
 *
 * @author CIAO
 */
public class UserJDBC extends SimpleJdbcDaoSupport implements dao.User{

    private String query = "";
    private static class UserMapper implements ParameterizedRowMapper<domain.User>{

        public domain.User mapRow(ResultSet rs, int i) throws SQLException {
            domain.User u = new User();
            u.setId(rs.getInt("Id"));
            u.setName(rs.getString("Name"));
            return u;
        }
    }

    public List<User> getAll() {
        try {
            query = "select * from tblUser";
            List<domain.User> list = this.getJdbcTemplate().query(query, new UserMapper());
            return list;
        } catch (Exception e) {
            System.out.println("Error in getAll() - UserJDBC class : "+e.getMessage());
            return null;
        }
    }

    public User getDetail(int id) {
        try {
            query = "select * from tblUser where id = "+id;
            List<domain.User> list = this.getJdbcTemplate().query(query, new UserMapper());
            return list.get(0);
        } catch (Exception e) {
            return null;
        }
    }

}
